package com.nulldev.util.web.nTubeExtensions.services;

import java.util.regex.Pattern;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.VariableAPI.StringsUtil;
import com.nulldev.util.data.Variables;
import com.nulldev.util.languages.json.uniapi.UniJsonValue;
import com.nulldev.util.web.HttpClient.HttpClient;

public class RaveDJ {

	private final String songID;
	private UniJsonValue data;

	public RaveDJ(final String songID) {
		Variables.notNullE(songID);
		if (songID.isEmpty())
			throw new IllegalArgumentException("songID is empty!");
		this.songID = songID;
	}

	public void init() throws Exception {
		this.init(this.songID);
	}

	private void init(final String songID) throws Exception {
		final String r = HttpClient.getString().url("https://rave.dj/" + songID).create().dataThenClose();
		final String jsonData = "{" + StringsUtil.substringBetweenO(r, "Env.dataStore = {", "} // Raw JSON").replaceAll(Pattern.quote("&quot;"), "\"") + "}";
		this.data = UniJsonValue.parse(jsonData).get("content").asArray().getJsonObject(0);
	}

	public String getMediaURL() {
		Variables.notNullE(this.data);
		return this.data.getString("videoUrl");
	}

	public String getAudioURL() {
		Variables.notNullE(this.data);
		return this.data.get("urls").getString("audio");
	}

	public String getTitle() {
		Variables.notNullE(this.data);
		return this.data.getString("title");
	}

	public String getArtist() {
		Variables.notNullE(this.data);
		return this.data.getString("artist");
	}

	public String getThumbnail() {
		Variables.notNullE(this.data);
		return this.data.getString("thumbnail");
	}

	public static void main(String[] args) throws Exception {
		final RaveDJ rdj = new RaveDJ("7WrsTK8EnhLxCw");
		rdj.init();
		JVM.println(rdj.getAudioURL());
	}
}
